#ifndef ADVIEWCONTROL_H
#define ADVIEWCONTROL_H

/*#include "AdserverRequest.h"*/
#include <QWebView>
#include <QBitmap>
#include <QString>
#include <QFile>

class AdViewControl;
class InterstitialDlg;
class AdserverRequest;

const int _nDispatchID = 1;
#define AD_RELOAD_PERIOD 120000
#define IDD_ADBROWSERVIEW 201
#define IDD_INTERSTITIALDLG 209

class AdViewControl : public QWebView
{
    Q_OBJECT

    public:
            enum{IDD = IDD_ADBROWSERVIEW};

    public:

            enum HORIZONTAL_ALIGNMENT
            {
                    HORZ_NONE = 0,
                    HORZ_LEFT = 1,
                    HORZ_RIGHT = 2,
                    HORZ_CENTER = 3
            };

            enum VERTICAL_ALIGNMENT
            {
                    VERT_NONE = 0,
                    VERT_TOP = 1,
                    VERT_BOTTOM = 2,
                    VERT_CENTER = 3
            };

            enum ADS_MODE
            {
                    /**
                    * The first-load ads counter is sent only. Ads is not shown.
                    */
                    MODE_COUNTER_ONLY = 1,
                    /**
                    * Ads is shown only. The first-load ads counter is not sent.
                    */
                    MODE_ADS_ONLY = 2,
                    /**
                    * The first-load ads counter is sent and ads is shown.
                    */
                    MODE_COUNTER_AND_ADS = 3
            };

            enum ADS_TYPE
            {
                    ADS_TYPE_UNDEFINED = 0,
                    /**
                    * Type of advertisement: text only.
                    */
                    ADS_TYPE_TEXT_ONLY = 1,
                    /**
                    * Type of advertisement: image only.
                    */
                    ADS_TYPE_IMAGES_ONLY = 2,
                    /**
                    * Type of advertisement: image and text.
                    */
                    ADS_TYPE_TEXT_AND_IMAGES = 3,
                    /**
                    * Type of advertisement: SMS ad. SMS will be returned in XML.
                    */
                    ADS_TYPE_SMS = 6
            };

            enum OVER_18_TYPE
            {
                    OVER_18_TYPE_UNDEFINED = 0,
                    /**
                    * Type of over 18 content: deny over 18 content.
                    */
                    OVER_18_TYPE_DENY = 1,
                    /**
                    * Type of over 18 content: only over 18 content.
                    */
                    OVER_18_TYPE_ONLY = 2,
                    /**
                    * Type of over 18 content: allow all ads including over 18 content.
                    */
                    OVER_18_TYPE_ALL = 3
            };

            enum PREMIUM_TYPE
            {
                    /*PREMIUM_STATUS_UNDEFINED = 0,*/
                    /**
                    * Premium type: premium and non-premium.
                    */
                    PREMIUM_STATUS_BOTH = 2,
                    /**
                    * Premium type: premium only.
                    */
                    PREMIUM_STATUS_PREMIUM = 1,
                    /**
                    * Premium type: non-premium.
                    */
                    PREMIUM_STATUS_NON_PREMIUM = 0
            };

            enum OUTPUT_FORMAT
            {
                    OUTPUT_FORMAT_UNDEFINED = 0,
                    /**
                    * Type of output format: normal.
                    */
                    OUTPUT_FORMAT_NORMAL = 1,
                    /**
                    * Type of output format: XML.
                    */
                    OUTPUT_FORMAT_XML = 3,
                    /**
                    * Type of output format: JSON.
                    */
                    OUTPUT_FORMAT_JSON = 5
            };

            enum TARGET_TYPE
            {
                    TARGET_UNDEFINED = 0,
                    /**
                    * Type of target attribute for HTML link element: open the linked document in a new window.
                    */
                    TARGET_BLANK = 1,
                    /**
                    * Type of target attribute for HTML link element: open the linked document in the parent frameset.
                    */
                    TARGET_PARENT = 2,
                    /**
                    * Type of target attribute for HTML link element: open the linked document in the same frame.
                    */
                    TARGET_SELF = 3,
                    /**
                    * Type of target attribute for HTML link element: open the linked document in the full body of the window.
                    */
                    TARGET_TOP = 4
            };

            AdViewControl(QWidget *parent = 0);


    protected:

            QString ReplaceImages( QString src );
            QSize GetImageSize( QString src );
            QString GetVideoUrl( QString src );
            QString GetReference( QString src );
            QString GetCachePath();

    public:

            bool  get_InternalBrowser();
            void  set_InternalBrowser(bool val);

            QBitmap  get_DefaultImage();
            void  set_DefaultImage(QBitmap val);

            int  get_UpdateTime();
            void  set_UpdateTime(int val);

            QString  get_Site();
            void  set_Site(QString val);

            QString  get_Zone();
            void  set_Zone(QString val);

            QString  get_Keywords();
            void  set_Keywords(QString val);

            int  get_AdsType();
            void  set_AdsType( int val );

            QString  get_Latitude();
            void  set_Latitude( QString val );

            QString  get_Longitude();
            void  set_Longitude(QString val);
            int  get_Premium();
            void  set_Premium(int val);

            bool  get_Test();
            void  set_Test(bool val);

            QString  get_Country();
            void  set_Country(QString val);

            QString  get_Region();
            void  set_Region( QString val);

            QString  get_City();
            void  set_City( QString val);

            QString  get_Area();
            void  set_Area( QString val);

            QString  get_Metro();
            void  set_Metro( QString val);

            QString  get_ZIP();
            void  set_ZIP( QString val);

            bool  get_TextBorderEnabled();
            void  set_TextBorderEnabled(bool val);

            QString  get_BorderColor();
            void  set_BorderColor(QString val);

            QString  get_BackgroundColor();
            void  set_BackgroundColor(QString val);

            QString  get_TextColor();
            void  set_TextColor(QString val);

            QString  get_Carrier();
            void  set_Carrier( QString val );
            int  get_MinSizeX();
            void  set_MinSizeX(int val);

            int  get_MinSizeY();
            void  set_MinSizeY(int val);

            int  get_MaxSizeX();
            void  set_MaxSizeX(int val);

            int  get_MaxSizeY();
            void  set_MaxSizeY(int val);

            QString  get_AdServerUrl();
            void  set_AdServerUrl(QString val);

            QString  get_AdvertiserId();
            void  set_AdvertiserId(QString val);

            QString  get_GroupCode();
            void  set_GroupCode(QString val);

            QString  get_CustomParameters();
            void  set_CustomParameters(QString val);

            HORIZONTAL_ALIGNMENT  get_HorizontalAlignment();
            void  set_HorizontalAlignment(HORIZONTAL_ALIGNMENT val);

            VERTICAL_ALIGNMENT  get_VerticalAlignment();
            void  set_VerticalAlignment(VERTICAL_ALIGNMENT val);

            bool  get_DisplayRichMediaType();
            void  set_DisplayRichMediaType(bool bVal);

            bool  get_GenerateRawImageEvent();
            void  set_GenerateRawImageEvent(bool bVal);

    public:

    #pragma region Deprecated properties
            /// <summary>
            /// Required.
            /// View id (default 1)
            /// </summary>
            Q_PROPERTY(int ViewId READ get_ViewId WRITE set_ViewId)


/**
            /// <summary>
            /// The period of an automatic reload of advertising (in milliseconds).
            /// (default = 120000) (min = 30000)
            /// </summary>
            Q_PROPERTY(int ReloadPeriod READ get_ViewId WRITE set_ReloadPeriod)


            /// <summary>
            /// Required.
            /// The id of the application.
            /// </summary>
            Q_PROPERTY(QString AppId READ get_AppId WRITE set_AppId)

            /// <summary>
            /// Campaign name
            /// </summary>
            Q_PROPERTY(QString Campaign READ get_Campaign WRITE set_Campaign)

            /// <summary>
            /// Required.
            /// Mode of viewer of advertising (use 1 - counter only, 2 - ads only, 3 - counter and ads)
            /// (default = 2)
            /// </summary>
            Q_PROPERTY(int Mode READ get_Mode WRITE set_Mode)

            /// <summary>
            /// The id of the publisher site.
            /// </summary>
            Q_PROPERTY(QString SiteId READ get_SiteId WRITE set_SiteId)

            /// <summary>
            /// The id of the zone of publisher site.
            /// </summary>
            Q_PROPERTY(QString ZoneId READ get_ZoneId WRITE set_ZoneId)


            /// <summary>
            /// The IP address of the carrier gateway over which the device is connecting.
            /// Autodetected if not set.
            /// </summary>
            Q_PROPERTY(QString Ip READ get_Ip WRITE set_Ip)

            /// <summary>
            /// Filter by ad over 18 content (0 - disabled, 1 - deny over 18 content , 2 - only over 18 content, 3 - allow all ads including over 18 content).
            /// (default = 0)
            /// </summary>
            Q_PROPERTY(int Over18 READ get_Over18 WRITE set_Over18)

            /// <summary>
            /// The browser user agent of the device making the request.
            /// </summary>
            Q_PROPERTY(QString UserAgent READ get_UserAgent WRITE set_UserAgent)

            /// <summary>
            /// Output format. Undefined = 0. Normal format uses key = 1. Parameter key should be set to 2 in order to use XML output and to 3 in order to use JSON output.
            /// (default = 0)
            /// </summary>
            Q_PROPERTY(int OutputFormat READ get_OutputFormat WRITE set_OutputFormat)

            /// <summary>
            /// Setting is test mode where, if the ad code is true, the ad response is "Test MODE".
            /// (default = false)
            /// </summary>
            Q_PROPERTY(bool TestModeEnabled READ get_TestModeEnabled WRITE set_TestModeEnabled)

            /// <summary>
            /// Quantity of ads, returned by a server. Maximum value is 5.
            /// (default = 1)
            /// </summary>
            Q_PROPERTY(int Count READ get_Count WRITE set_Count)

            /// <summary>
            /// Show borders around text ads (false - non-borders, true - show borders).
            /// (default = false)
            /// </summary>
            Q_PROPERTY(bool TextBorderEnabled READ get_TextBorderEnabled WRITE set_TextBorderEnabled)

            /// <summary>
            /// Borders color
            /// </summary>
            Q_PROPERTY(QString BorderColor READ get_BorderColor WRITE set_BorderColor)

            /// <summary>
            /// Min banner width
            /// </summary>
            Q_PROPERTY(int ImageMinWidth READ get_ImageMinWidth WRITE set_ImageMinWidth)

            /// <summary>
            /// Min banner height
            /// </summary>
            Q_PROPERTY(int ImageMinHeight READ get_ImageMinHeight WRITE set_ImageMinHeight)

            /// <summary>
            /// Max banner width
            /// </summary>
            Q_PROPERTY(int ImageMaxWidth READ get_ImageMaxWidth WRITE set_ImageMaxWidth)

            /// <summary>
            /// Max banner height
            /// </summary>
            Q_PROPERTY(int ImageMaxHeight READ get_ImageMaxHeight WRITE set_ImageMaxHeight)

            /// <summary>
            /// Target attribute for HTML link element (0 - undefined, 1 - open the linked document in a new window, 2 - open the linked document in the same frame, 3 - open the linked document in the parent frameset, 4 - open the linked document in the full body of the window).
            /// (default = 0)
            /// </summary>
            Q_PROPERTY(int Target READ get_Target WRITE set_Target)

            /// <summary>
            /// If true, return redirect to image for ad (or tracking pixel 1x1) directly (instead of html response).
            /// (default = false)
            /// </summary>
            Q_PROPERTY(bool PixelModeEnabled READ get_PixelModeEnabled WRITE set_PixelModeEnabled)



*/

            /// <summary>
            /// Set custom parameter
            /// </summary>
            /// <param name="strName">Custom parameter name</param>
            /// <param name="strValue">Custom parameter value</param>
            /// <return></return>
            void SetCustomParameter( QString strName, QString strValue );

/**


            /// <summary>
            /// Remove custom parameter
            /// </summary>
            /// <param name="nIdx">Custom parameter index</param>
            /// <return>TRUE if found and removed, otherwise FALSE</return>
            bool RemoveCustomParameter( int nIdx );

            /// <summary>
            /// Get custom parameter
            /// </summary>
            /// <param name="nIdx">Custom parameter index</param>
            /// <return>Custom parameter value (if found, otherwise empty string)</return>
            QString GetCustomParameter( int nIdx );

            /// <summary>
            /// Run banner engine
            /// </summary>
            void Run();
*/
    #pragma endregion Deprecated properties

    #pragma region API 2.0 Properties
            /// <summary>
            /// The id of the publisher site.
            /// </summary>
            Q_PROPERTY(QString Site READ get_Site WRITE set_Site)

            /// <summary>
            /// The id of the zone of publisher site.
            /// </summary>
            Q_PROPERTY(QString Zone READ get_Zone WRITE set_Zone)

            /// <summary>
            /// Required.
            /// Type of advertisement (0 - disabled, 1 - text only, 2 - image only, 3 - image and text, 4 - SMS ad). SMS will be returned in XML.
            /// (default = 3)
            /// </summary>
            Q_PROPERTY(int AdsType READ get_AdsType WRITE set_AdsType)

            /// <summary>
            /// The flag which operates advertising opening. False - Ad opens in an external browser.
            /// True - Ad opening at internal form.
            /// (default = false)
            /// </summary>
            Q_PROPERTY(bool InternalBrowser READ get_InternalBrowser WRITE set_InternalBrowser)

            /// <summary>
            /// Image which will be shown during advertising loading if there is no advertising in a cache.
            /// (default = none)
            /// </summary>
            Q_PROPERTY(QPixmap DefaultImage READ get_DefaultImage WRITE set_DefaultImage)

            /// <summary>
            /// The period of an automatic reload of advertising (in seconds).
            /// (default = 120) (min = 30)
            /// </summary>
            Q_PROPERTY(int UpdateTime READ get_UpdateTime WRITE set_UpdateTime)

            /// <summary>
            /// Keywords to search ad delimited by commas.
            /// </summary>
            Q_PROPERTY(QString Keywords READ get_Keywords WRITE set_Keywords)

            /// <summary>
            /// Latitude.
            /// Autodetected if not set.
            /// </summary>
            Q_PROPERTY(QString Latitude READ get_Latitude WRITE set_Latitude)

            /// <summary>
            /// Longitude.
            /// Autodetected if not set.
            /// </summary>
            Q_PROPERTY(QString Longitude READ get_Longitude WRITE set_Longitude)

            /// <summary>
            /// Filter by premium (0 - disabled, 1 - non-premium, 2 - premium only, 3 - both). Can be used only by premium publishers.
            /// (default = 0)
            /// </summary>
            Q_PROPERTY(int Premium READ get_Premium WRITE set_Premium)

            /// <summary>
            /// Setting is test mode where, if the ad code is true, the ad response is "Test MODE".
            /// (default = false)
            /// </summary>
            Q_PROPERTY(bool Test READ get_Test WRITE set_Test)

            /// <summary>
            /// Country of visitor (for example: US).
            /// Autodetected if not set.
            /// </summary>
            Q_PROPERTY(QString Country READ get_Country WRITE set_Country)

            /// <summary>
            /// Region of visitor (for example: NY).
            /// </summary>
            Q_PROPERTY(QString Region READ get_Region WRITE set_Region)

            /// <summary>
            /// City
            /// </summary>
            Q_PROPERTY(QString City READ get_City WRITE set_City)

            /// <summary>
            /// Area
            /// </summary>
            Q_PROPERTY(QString Area READ get_Area WRITE set_Area)

            /// <summary>
            /// Metro
            /// </summary>
            Q_PROPERTY(QString Metro READ get_Metro WRITE set_Metro)

            /// <summary>
            /// Zip
            /// </summary>
            Q_PROPERTY(QString ZIP READ get_ZIP WRITE set_ZIP)

            /// <summary>
            /// Background color in borders
            /// </summary>
            Q_PROPERTY(QString BackgroundColor READ get_BackgroundColor WRITE set_BackgroundColor)

            /// <summary>
            /// Text color
            /// </summary>
            Q_PROPERTY(QString TextColor READ get_TextColor WRITE set_TextColor)

            /// <summary>
            /// Carrier name.
            /// </summary>
            Q_PROPERTY(QString Carrier READ get_Carrier WRITE set_Carrier)

            /// <summary>
            /// Min banner width
            /// </summary>
            Q_PROPERTY(int MinSizeX READ get_MinSizeX WRITE set_MinSizeX)

            /// <summary>
            /// Min banner height
            /// </summary>
            Q_PROPERTY(int MinSizeY READ get_MinSizeY WRITE set_MinSizeY)

            /// <summary>
            /// Max banner width
            /// </summary>
            Q_PROPERTY(int MaxSizeX READ get_MaxSizeX WRITE set_MaxSizeX)

            /// <summary>
            /// Max banner height
            /// </summary>
            Q_PROPERTY(int MaxSizeY READ get_MaxSizeY WRITE set_MaxSizeY)

            /// <summary>
            /// Url of banners server
            /// </summary>
            Q_PROPERTY(QString AdServerUrl READ get_AdServerUrl WRITE set_AdServerUrl)

            /// <summary>
            /// Advertiser Id
            /// </summary>
            Q_PROPERTY(QString AdvertiserId READ get_AdvertiserId WRITE set_AdvertiserId)

            /// <summary>
            /// Group Code
            /// </summary>
            Q_PROPERTY(QString GroupCode READ get_GroupCode WRITE set_GroupCode)

            /// <summary>
            /// Custom parameters
            /// </summary>
            Q_PROPERTY(QString CustomParameters READ get_CustomParameters WRITE set_CustomParameters)

            /// <summary>
            /// Horizontal alignment
            /// </summary>
            Q_PROPERTY(HORIZONTAL_ALIGNMENT HorizontalAlignment READ get_HorizontalAlignment WRITE set_HorizontalAlignment)

            /// <summary>
            /// Vertical alignment
            /// </summary>
            Q_PROPERTY(VERTICAL_ALIGNMENT VerticalAlignment READ get_VerticalAlignment WRITE set_VerticalAlignment)

            /// <summary>
            /// Show or dont show Rich Media banner contents
            /// </summary>
            Q_PROPERTY(bool DisplayRichMediaType READ get_DisplayRichMediaType WRITE set_DisplayRichMediaType)

            /// <summary>
            /// Generate event with image texture to use in D3d/OpenGL environments
            /// </summary>
            Q_PROPERTY(bool GenerateRawImageEvent READ get_GenerateRawImageEvent WRITE set_GenerateRawImageEvent)
    #pragma endregion API 2.0 Properties

    #pragma region API 2.0 Methods
            /// <summary>
            /// Click
            /// </summary>
            void Click();

            /// <summary>
            /// Update banner
            /// </summary>
            void Update();
    #pragma endregion API 2.0 Methods

    #pragma region Protected Variables
    protected:

            //bool m_useCache;
            int m_viewId;
            bool m_Browser;
            int m_reloadPeriod;
            QString m_appId;
            QString m_campaign;
            ADS_MODE m_mode;
            bool m_testModeEnabled;
            bool m_textBorderEnabled;
            bool m_pixelModeEnabled;
            bool m_internalBrowser;
            QBitmap m_defaultImage;
            QString m_advertiserId;
            QString m_groupCode;
            AdserverRequest* m_request;
            QTimer* updateTimer;
            QString m_dataCurrent;
            QWebView m_cpWebBrowser2;

            bool m_visible;

    #pragma endregion Protected Variables

    signals:
    #pragma region Events

            void AdDownloadBegin();
            void AdDownloadEnd();
            void AdDownloadError(QString);
            void AdWebViewClosing(QString);


    #pragma endregion Events

    protected:
    #pragma region Internal methods and properties

            void Stop();
            void Start();
            QString PostProcessData( QString data );
            QString LoadOrmmaNamespace();
            void DownloadBannerSource();
            QString bodyBegin;
            QString bodyEnd;
            void Navigate( QString strUrl );
            bool IsWindowVisible();

            void ContentThread();

            bool m_bNavigate, m_bShowRichMedia, m_bGenerateRawImageEvent;
            QString m_strPlatform;
            QString m_strCpuManufacturer, m_strCpuModel, m_strCpuFrequency, m_strOSdescription, m_strOSversion, m_strOSpackage, m_strMotherboard, m_strVideo, m_strRam, m_strHDDManufacturer, m_strHDDModel, m_strHDDTotal, m_strHDDFree, m_strDeviceID;
            QString m_strBackgroundColor;
            HORIZONTAL_ALIGNMENT m_horzAlign;
            VERTICAL_ALIGNMENT m_vertAlign;


            bool m_bFirstRun;
            QByteArray *m_pImageArray;
            int m_nTextureWidth, m_nTextureHeight;
            bool m_expanded;
            QString m_expandedUrl;
            QString m_execScript;
            bool m_bRestart;
            QString m_strOrmmaNamespace;
            QString m_strReference;
            bool m_bFirstLaunch;
            QFile* file;
            QNetworkReply* reply;
    #pragma endregion Internal methods and properties

    #pragma region Friends

    #pragma endregion Friends
private slots:
    void httpFinished();
    void httpReadyRead();
    void onLinkClicked( const QUrl & url);
    void TimerTimeOut();
private:


    int  get_ViewId();
    void  set_ViewId(int val);

    int  get_ReloadPeriod();
    void  set_ReloadPeriod(int val);

    QString  get_AppId();
    void  set_AppId( QString val);

    QString  get_Campaign();
    void  set_Campaign(QString val);

    int  get_Mode();
    void  set_Mode( int val);

    QString  get_SiteId();
    void  set_SiteId(QString val);

    QString  get_ZoneId();
    void  set_ZoneId(QString val);

    QString  get_Ip();
    void  set_Ip( QString val );

    int  get_Over18();
    void  set_Over18(int val );

    QString  get_UserAgent();
    void  set_UserAgent(QString val);


    int  get_OutputFormat();
    void  set_OutputFormat(int val);

    bool  get_TestModeEnabled();
    void  set_TestModeEnabled(bool val);

    int  get_Count();
    void  set_Count(int val);

    int  get_ImageMinWidth();
    void  set_ImageMinWidth(int val);

    int  get_ImageMinHeight();
    void  set_ImageMinHeight(int val);

    int  get_ImageMaxWidth();
    void  set_ImageMaxWidth(int val);

    int  get_ImageMaxHeight();
    void  set_ImageMaxHeight(int val);

    int  get_Target();
    void  set_Target( int val );

    bool  get_PixelModeEnabled();
    void  set_PixelModeEnabled(bool val);

    /// <summary>
    /// Set custom parameters array
    /// </summary>
    void SetCustomParametersArray(QStringList paramPairs);

    /// <summary>
    /// Remove custom parameter
    /// </summary>
    /// <param name="strName">Custom parameter name</param>
    /// <return>TRUE if found and removed, otherwise FALSE</return>
    bool RemoveCustomParameter( QString strName );

    /// <summary>
    /// Get custom parameter
    /// </summary>
    /// <param name="strName">Custom parameter name</param>
    /// <return>Custom parameter value (if found, otherwise empty string)</return>
    QString GetCustomParameter( QString strName );
    };



#endif // ADVIEWCONTROL_H
